import { ApiLogin } from "./api/api.login";
import { StorageTokenKey, StorageUserKey } from "./utils/config";
import { ApiLoginReqInterface, ApiLoginResInterface } from "./interface/api.login.interface";
import { ResInterface } from "./interface/request.interface";
import { AsyncShowToast, CheckSession, GetAppId, Login, SetLocal } from "./utils/common";

// app.ts
App<IAppOption>({
  globalData: {
    token: '',
    code: '',
    userInfo: {
      avatarUrl: '',
      city: '',
      country: '',
      gender: 0,
      language: 'zh_CN',
      nickName: '',
      province: '',
    },
  },
  async onLaunch() {
    try {
      const _Login: string = await Login();
      console.log('客户端获取的', _Login);
      const _GetAppId: string = await GetAppId();
      console.log('AppId', _GetAppId)
      const _data: ApiLoginReqInterface = {
        app_id: _GetAppId,
        code: _Login,
      };
      console.log('登录参数', _data)
      const _ApiLogin: ResInterface = await ApiLogin({
        data: _data
      });
      console.log('登录返回值', _ApiLogin)
      const { code, data, message } = _ApiLogin;
      if (code === 200) {
        const _data: ApiLoginResInterface = {
          token: data['token'] || '',
        };
        await SetLocal(StorageTokenKey, _data);
        return true;
      } else {
        throw message || '登录接口请求失败';
      }
    } catch (error) {
      await AsyncShowToast(error);
    }
  },
})